package cn.yhbk.web.modules.manager.Service.Impl;

import cn.hutool.core.bean.BeanUtil;
import cn.yhbk.web.common.exception.ApiException;
import cn.yhbk.web.modules.manager.Pojo.DTO.Counsellor.CounsellorCondition;
import cn.yhbk.web.modules.manager.Pojo.Entity.TbClass;
import cn.yhbk.web.modules.manager.Pojo.Entity.TbCounsellor;
import cn.yhbk.web.modules.manager.Mapper.TbCounsellorMapper;
import cn.yhbk.web.modules.manager.Pojo.Entity.TbStudent;
import cn.yhbk.web.modules.manager.Pojo.VO.Counsellor.CounsellorAdd;
import cn.yhbk.web.modules.manager.Service.IFlagService;
import cn.yhbk.web.modules.manager.Service.ITbClassService;
import cn.yhbk.web.modules.manager.Service.ITbCounsellorService;
import cn.yhbk.web.modules.manager.Service.ITbStudentService;
import cn.yhbk.web.modules.ums.dto.UmsAdminParam;
import cn.yhbk.web.modules.ums.model.UmsAdmin;
import cn.yhbk.web.modules.ums.service.UmsAdminService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 辅导员表 服务实现类
 * </p>
 *
 * @author 有何不可
 * @since 2023-08-05
 */
@Service
@Slf4j
public class TbCounsellorServiceImpl extends ServiceImpl<TbCounsellorMapper, TbCounsellor> implements ITbCounsellorService {

    @Autowired
    UmsAdminService adminService;

    @Autowired
    IFlagService flagService;

    @Autowired
    private ITbClassService classService;

    @Autowired
    private ITbStudentService studentService;

    @Override
    @Transactional
    public boolean addCounsellor(CounsellorAdd add) {
        //添加账号
        UmsAdminParam umsAdminParam = new UmsAdminParam();
        BeanUtil.copyProperties(add,umsAdminParam);
        umsAdminParam.setUsername(add.getJobNumber());
        UmsAdmin register = adminService.register(umsAdminParam);
        //拿到adminId
        if(Objects.isNull(register)){
            log.error("账号添加失败!");
            throw new ApiException("添加失败!");
        }
        Long adminId = register.getId();


        //添加辅导员
        TbCounsellor counsellor = new TbCounsellor();
        BeanUtil.copyProperties(add,counsellor);
        counsellor.setAdminId(adminId);
        boolean save = save(counsellor);
        if(!save){
            log.error("信息添加失败!");
            throw new ApiException("添加失败!");
        }

        //设置角色
        Long counsellorRoleId = flagService.getCounsellorRoleId();
        int updateRole = adminService.updateRole(adminId, Collections.singletonList(counsellorRoleId));
        if(updateRole == 0){
            log.error("设置角色失败!");
            throw new ApiException("添加失败!");
        }
        return true;
    }

    @Override
    public Page<TbCounsellor> getCounsellor(Long currentPage, Long pageSize, CounsellorCondition condition) {
        Page<TbCounsellor> page = new Page<>(currentPage,pageSize);

        QueryWrapper<TbCounsellor> wrapper = new QueryWrapper<>();
        LambdaQueryWrapper<TbCounsellor> lambda = wrapper.lambda();

        Long id = condition.getId();
        if(!Objects.isNull(id))lambda.eq(TbCounsellor::getId,id);

        String name = condition.getName();
        if(!Strings.isBlank(name))lambda.like(TbCounsellor::getName,name);

        String phone = condition.getPhone();
        if(!Strings.isBlank(phone))lambda.like(TbCounsellor::getPhone,phone);

        String email = condition.getEmail();
        if(!Strings.isBlank(phone))lambda.like(TbCounsellor::getEmail,email);

        Byte status = condition.getStatus();
        if(!Objects.isNull(status))lambda.eq(TbCounsellor::getStatus,status);

        List<Long> adminIds = condition.getAdminIds();
        if(!Objects.isNull(adminIds) && adminIds.isEmpty())adminIds.add(-1L);
        if(!Objects.isNull(adminIds))lambda.in(TbCounsellor::getAdminId,adminIds);

        return page(page, lambda);
    }

    @Override
    public boolean updateByAdminId(Long currentAdminId, TbCounsellor counsellor) {
        QueryWrapper<TbCounsellor> wrapper = new QueryWrapper<>();
        LambdaQueryWrapper<TbCounsellor> lambda = wrapper.lambda();

        //添加条件
        lambda.eq(TbCounsellor::getAdminId,currentAdminId);

        //修改
        return update(counsellor,lambda);
    }

    @Override
    public boolean removeCounsellor(Long counsellorId) {
        TbCounsellor counsellor = getById(counsellorId);
        if(Objects.isNull(counsellor))return true;
        Long adminId = counsellor.getAdminId();

        //删除账号
        boolean delete = adminService.delete(adminId);
        if(!delete){
            log.error("账号删除失败!");
            throw new ApiException("删除失败!");
        }

        //删除信息
        boolean remove = removeById(counsellorId);
        if(!remove){
            log.error("信息删除失败!");
            throw new ApiException("删除失败!");
        }
        return true;
    }

    @Override
    public TbCounsellor getCounsellorByAdminId(Long adminId) {

        QueryWrapper<TbCounsellor> wrapper = new QueryWrapper<>();
        LambdaQueryWrapper<TbCounsellor> lambda = wrapper.lambda();

        lambda.eq(TbCounsellor::getAdminId,adminId);
        List<TbCounsellor> list = list(lambda);
        if(list.size() != 1){
            log.error("一个adminId有多个辅导员信息或者根据adminId查询为空");
            throw new ApiException("出错啦，请联系辅导员!");
        }

        return list.get(0);
    }

    @Transactional
    @Override
    public Boolean giveClass(Long counsellorId, List<Long> classIds) {

        this.baseMapper.giveClass(counsellorId, classIds);

        // 修改学生对应的辅导员Id
        studentService.updateCounsellorIdByClassId(counsellorId, classIds);

        return true;
    }

    @Override
    public List<TbClass> getMyClass(Long counsellorId) {

        return classService.list(new QueryWrapper<TbClass>().eq("counsellor_id", counsellorId));
    }
}
